home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / gawk-3.000 / gawk-3 / gawk-3.0.0 / missing / strftime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-11  |  21.3 KB  |  890 lines

  1. /*
  2.  * strftime.c
  3.  *
  4.  * Public-domain implementation of ANSI C library routine.
  5.  *
  6.  * It's written in old-style C for maximal portability.
  7.  * However, since I'm used to prototypes, I've included them too.
  8.  *
  9.  * If you want stuff in the System V ascftime routine, add the SYSV_EXT define.
  10.  * For extensions from SunOS, add SUNOS_EXT.
  11.  * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
  12.  * For VMS dates, add VMS_EXT.
  13.  * For a an RFC822 time format, add MAILHEADER_EXT.
  14.  * For ISO week years, add ISO_DATE_EXT.
  15.  * For complete POSIX semantics, add POSIX_SEMANTICS.
  16.  *
  17.  * The code for %c, %x, and %X now follows the 1003.2 specification for
  18.  * the POSIX locale.
  19.  * This version ignores LOCALE information.
  20.  * It also doesn't worry about multi-byte characters.
  21.  * So there.
  22.  *
  23.  * This file is also shipped with GAWK (GNU Awk), gawk specific bits of
  24.  * code are included if GAWK is defined.
  25.  *
  26.  * Arnold Robbins
  27.  * January, February, March, 1991
  28.  * Updated March, April 1992
  29.  * Updated April, 1993
  30.  * Updated February, 1994
  31.  * Updated May, 1994
  32.  * Updated January, 1995
  33.  * Updated September, 1995
  34.  * Updated January, 1996
  35.  *
  36.  * Fixes from ado@elsie.nci.nih.gov
  37.  * February 1991, May 1992
  38.  * Fixes from Tor Lillqvist tml@tik.vtt.fi
  39.  * May, 1993
  40.  * Further fixes from ado@elsie.nci.nih.gov
  41.  * February 1994
  42.  * %z code from chip@chinacat.unicom.com
  43.  * Applied September 1995
  44.  * %V code fixed (again) and %G, %g added,
  45.  * January 1996
  46.  */
  47.  
  48. #ifndef GAWK
  49. #include <stdio.h>
  50. #include <ctype.h>
  51. #include <string.h>
  52. #include <time.h>
  53. #endif
  54. #if defined(TM_IN_SYS_TIME) || ! defined(GAWK)
  55. #include <sys/types.h>
  56. #include <sys/time.h>
  57. #endif
  58.  
  59. /* defaults: season to taste */
  60. #define SYSV_EXT    1    /* stuff in System V ascftime routine */
  61. #define SUNOS_EXT    1    /* stuff in SunOS strftime routine */
  62. #define POSIX2_DATE    1    /* stuff in Posix 1003.2 date command */
  63. #define VMS_EXT        1    /* include %v for VMS date format */
  64. #define MAILHEADER_EXT    1    /* add %z for HHMM format */
  65. #define ISO_DATE_EXT    1    /* %G and %g for year of ISO week */
  66. #ifndef GAWK
  67. #define POSIX_SEMANTICS    1    /* call tzset() if TZ changes */
  68. #endif
  69.  
  70. #if defined(ISO_DATE_EXT)
  71. #if ! defined(POSIX2_DATE)
  72. #define POSIX2_DATE    1
  73. #endif
  74. #endif
  75.  
  76. #if defined(POSIX2_DATE)
  77. #if ! defined(SYSV_EXT)
  78. #define SYSV_EXT    1
  79. #endif
  80. #if ! defined(SUNOS_EXT)
  81. #define SUNOS_EXT    1
  82. #endif
  83. #endif
  84.  
  85. #if defined(POSIX2_DATE)
  86. #define adddecl(stuff)    stuff
  87. #else
  88. #define adddecl(stuff)
  89. #endif
  90.  
  91. #undef strchr    /* avoid AIX weirdness */
  92.  
  93. #ifndef __STDC__
  94. #define const    /**/
  95. extern void *malloc();
  96. extern void *realloc();
  97. extern void tzset();
  98. extern char *strchr();
  99. extern char *getenv();
  100. static int weeknumber();
  101. adddecl(static int iso8601wknum();)
  102. #else
  103. extern void *malloc(unsigned count);
  104. extern void *realloc(void *ptr, unsigned count);
  105. extern void tzset(void);
  106. extern char *strchr(const char *str, int ch);
  107. extern char *getenv(const char *v);
  108. static int weeknumber(const struct tm *timeptr, int firstweekday);
  109. adddecl(static int iso8601wknum(const struct tm *timeptr);)
  110. #endif
  111.  
  112. #ifdef __GNUC__
  113. #define inline    __inline__
  114. #else
  115. #define inline    /**/
  116. #endif
  117.  
  118. #define range(low, item, hi)    max(low, min(item, hi))
  119.  
  120. #if !defined(OS2) && !defined(MSDOS) && defined(HAVE_TZNAME)
  121. extern char *tzname[2];
  122. extern int daylight;
  123. #ifdef SOLARIS
  124. extern long timezone, altzone;
  125. #else
  126. extern int timezone, altzone;
  127. #endif
  128. #endif
  129.  
  130. #undef min    /* just in case */
  131.  
  132. /* min --- return minimum of two numbers */
  133.  
  134. #ifndef __STDC__
  135. static inline int
  136. min(a, b)
  137. int a, b;
  138. #else
  139. static inline int
  140. min(int a, int b)
  141. #endif
  142. {
  143.     return (a < b ? a : b);
  144. }
  145.  
  146. #undef max    /* also, just in case */
  147.  
  148. /* max --- return maximum of two numbers */
  149.  
  150. #ifndef __STDC__
  151. static inline int
  152. max(a, b)
  153. int a, b;
  154. #else
  155. static inline int
  156. max(int a, int b)
  157. #endif
  158. {
  159.     return (a > b ? a : b);
  160. }
  161.  
  162. /* strftime --- produce formatted time */
  163.  
  164. #ifndef __STDC__
  165. size_t
  166. strftime(s, maxsize, format, timeptr)
  167. char *s;
  168. size_t maxsize;
  169. const char *format;
  170. const struct tm *timeptr;
  171. #else
  172. size_t
  173. strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
  174. #endif
  175. {
  176.     char *endp = s + maxsize;
  177.     char *start = s;
  178.     auto char tbuf[100];
  179.     long off;
  180.     int i, w, y;
  181.     static short first = 1;
  182. #ifdef POSIX_SEMANTICS
  183.     static char *savetz = NULL;
  184.     static int savetzlen = 0;
  185.     char *tz;
  186. #endif /* POSIX_SEMANTICS */
  187. #ifndef HAVE_TM_ZONE
  188. #ifndef HAVE_TM_NAME
  189. #ifndef HAVE_TZNAME
  190.     extern char *timezone();
  191.     struct timeval tv;
  192.     struct timezone zone;
  193. #endif /* HAVE_TZNAME */
  194. #endif /* HAVE_TM_NAME */
  195. #endif /* HAVE_TM_ZONE */
  196.  
  197.     /* various tables, useful in North America */
  198.     static const char *days_a[] = {
  199.         "Sun", "Mon", "Tue", "Wed",
  200.         "Thu", "Fri", "Sat",
  201.     };
  202.     static const char *days_l[] = {
  203.         "Sunday", "Monday", "Tuesday", "Wednesday",
  204.         "Thursday", "Friday", "Saturday",
  205.     };
  206.     static const char *months_a[] = {
  207.         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  208.         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  209.     };
  210.     static const char *months_l[] = {
  211.         "January", "February", "March", "April",
  212.         "May", "June", "July", "August", "September",
  213.         "October", "November", "December",
  214.     };
  215.     static const char *ampm[] = { "AM", "PM", };
  216.  
  217.     if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
  218.         return 0;
  219.  
  220.     /* quick check if we even need to bother */
  221.     if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
  222.         return 0;
  223.  
  224. #ifndef POSIX_SEMANTICS
  225.     if (first) {
  226.         tzset();
  227.         first = 0;
  228.     }
  229. #else    /* POSIX_SEMANTICS */
  230.     tz = getenv("TZ");
  231.     if (first) {
  232.         if (tz != NULL) {
  233.             int tzlen = strlen(tz);
  234.  
  235.             savetz = (char *) malloc(tzlen + 1);
  236.             if (savetz != NULL) {
  237.                 savetzlen = tzlen + 1;
  238.                 strcpy(savetz, tz);
  239.             }
  240.         }
  241.         tzset();
  242.         first = 0;
  243.     }
  244.     /* if we have a saved TZ, and it is different, recapture and reset */
  245.     if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
  246.         i = strlen(tz) + 1;
  247.         if (i > savetzlen) {
  248.             savetz = (char *) realloc(savetz, i);
  249.             if (savetz) {
  250.                 savetzlen = i;
  251.                 strcpy(savetz, tz);
  252.             }
  253.         } else
  254.             strcpy(savetz, tz);
  255.         tzset();
  256.     }
  257. #endif    /* POSIX_SEMANTICS */
  258.  
  259.     for (; *format && s < endp - 1; format++) {
  260.         tbuf[0] = '\0';
  261.         if (*format != '%') {
  262.             *s++ = *format;
  263.             continue;
  264.         }
  265.     again:
  266.         switch (*++format) {
  267.         case '\0':
  268.             *s++ = '%';
  269.             goto out;
  270.  
  271.         case '%':
  272.             *s++ = '%';
  273.             continue;
  274.  
  275.         case 'a':    /* abbreviated weekday name */
  276.             if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
  277.                 strcpy(tbuf, "?");
  278.             else
  279.                 strcpy(tbuf, days_a[timeptr->tm_wday]);
  280.             break;
  281.  
  282.         case 'A':    /* full weekday name */
  283.             if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
  284.                 strcpy(tbuf, "?");
  285.             else
  286.                 strcpy(tbuf, days_l[timeptr->tm_wday]);
  287.             break;
  288.  
  289. #ifdef SYSV_EXT
  290.         case 'h':    /* abbreviated month name */
  291. #endif
  292.         case 'b':    /* abbreviated month name */
  293.             if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
  294.                 strcpy(tbuf, "?");
  295.             else
  296.                 strcpy(tbuf, months_a[timeptr->tm_mon]);
  297.             break;
  298.  
  299.         case 'B':    /* full month name */
  300.             if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
  301.                 strcpy(tbuf, "?");
  302.             else
  303.                 strcpy(tbuf, months_l[timeptr->tm_mon]);
  304.             break;
  305.  
  306.         case 'c':    /* appropriate date and time representation */
  307.             strftime(tbuf, sizeof tbuf, "%a %b %e %H:%M:%S %Y", timeptr);
  308.             break;
  309.  
  310.         case 'd':    /* day of the month, 01 - 31 */
  311.             i = range(1, timeptr->tm_mday, 31);
  312.             sprintf(tbuf, "%02d", i);
  313.             break;
  314.  
  315.         case 'H':    /* hour, 24-hour clock, 00 - 23 */
  316.             i = range(0, timeptr->tm_hour, 23);
  317.             sprintf(tbuf, "%02d", i);
  318.             break;
  319.  
  320.         case 'I':    /* hour, 12-hour clock, 01 - 12 */
  321.             i = range(0, timeptr->tm_hour, 23);
  322.             if (i == 0)
  323.                 i = 12;
  324.             else if (i > 12)
  325.                 i -= 12;
  326.             sprintf(tbuf, "%02d", i);
  327.             break;
  328.  
  329.         case 'j':    /* day of the year, 001 - 366 */
  330.             sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
  331.             break;
  332.  
  333.         case 'm':    /* month, 01 - 12 */
  334.             i = range(0, timeptr->tm_mon, 11);
  335.             sprintf(tbuf, "%02d", i + 1);
  336.             break;
  337.  
  338.         case 'M':    /* minute, 00 - 59 */
  339.             i = range(0, timeptr->tm_min, 59);
  340.             sprintf(tbuf, "%02d", i);
  341.             break;
  342.  
  343.         case 'p':    /* am or pm based on 12-hour clock */
  344.             i = range(0, timeptr->tm_hour, 23);
  345.             if (i < 12)
  346.                 strcpy(tbuf, ampm[0]);
  347.             else
  348.                 strcpy(tbuf, ampm[1]);
  349.             break;
  350.  
  351.         case 'S':    /* second, 00 - 61 */
  352.             i = range(0, timeptr->tm_sec, 61);
  353.             sprintf(tbuf, "%02d", i);
  354.             break;
  355.  
  356.         case 'U':    /* week of year, Sunday is first day of week */
  357.             sprintf(tbuf, "%02d", weeknumber(timeptr, 0));
  358.             break;
  359.  
  360.         case 'w':    /* weekday, Sunday == 0, 0 - 6 */
  361.             i = range(0, timeptr->tm_wday, 6);
  362.             sprintf(tbuf, "%d", i);
  363.             break;
  364.  
  365.         case 'W':    /* week of year, Monday is first day of week */
  366.             sprintf(tbuf, "%02d", weeknumber(timeptr, 1));
  367.             break;
  368.  
  369.         case 'x':    /* appropriate date representation */
  370.             strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
  371.             break;
  372.  
  373.         case 'X':    /* appropriate time representation */
  374.             strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
  375.             break;
  376.  
  377.         case 'y':    /* year without a century, 00 - 99 */
  378.             i = timeptr->tm_year % 100;
  379.             sprintf(tbuf, "%02d", i);
  380.             break;
  381.  
  382.         case 'Y':    /* year with century */
  383.             sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
  384.             break;
  385.  
  386. #ifdef MAILHEADER_EXT
  387.         /*
  388.          * From: Chip Rosenthal <chip@chinacat.unicom.com>
  389.          * Date: Sun, 19 Mar 1995 00:33:29 -0600 (CST)
  390.          * 
  391.          * Warning: the %z [code] is implemented by inspecting the
  392.          * timezone name conditional compile settings, and
  393.          * inferring a method to get timezone offsets. I've tried
  394.          * this code on a couple of machines, but I don't doubt
  395.          * there is some system out there that won't like it.
  396.          * Maybe the easiest thing to do would be to bracket this
  397.          * with an #ifdef that can turn it off. The %z feature
  398.          * would be an admittedly obscure one that most folks can
  399.          * live without, but it would be a great help to those of
  400.          * us that muck around with various message processors.
  401.          */
  402.          case 'z':    /* time zone offset east of GMT e.g. -0600 */
  403. #ifdef HAVE_TM_NAME
  404.             /*
  405.              * Systems with tm_name probably have tm_tzadj as
  406.              * secs west of GMT.  Convert to mins east of GMT.
  407.              */
  408.             off = -timeptr->tm_tzadj / 60;
  409. #else /* !HAVE_TM_NAME */
  410. #ifdef HAVE_TM_ZONE
  411.             /*
  412.              * Systems with tm_zone probably have tm_gmtoff as
  413.              * secs east of GMT.  Convert to mins east of GMT.
  414.              */
  415.             off = timeptr->tm_gmtoff / 60;
  416. #else /* !HAVE_TM_ZONE */
  417. #if HAVE_TZNAME
  418.             /*
  419.              * Systems with tzname[] probably have timezone as
  420.              * secs west of GMT.  Convert to mins east of GMT.
  421.              */
  422.             off = -(daylight ? timezone : altzone) / 60;
  423. #else /* !HAVE_TZNAME */
  424.             off = -zone.tz_minuteswest;
  425. #endif /* !HAVE_TZNAME */
  426. #endif /* !HAVE_TM_ZONE */
  427. #endif /* !HAVE_TM_NAME */
  428.             if (off < 0) {
  429.                 tbuf[0] = '-';
  430.                 off = -off;
  431.             } else {
  432.                 tbuf[0] = '+';
  433.             }
  434.             sprintf(tbuf+1, "%02d%02d", off/60, off%60);
  435.             break;
  436. #endif /* MAILHEADER_EXT */
  437.  
  438.         case 'Z':    /* time zone name or abbrevation */
  439. #ifdef HAVE_TZNAME
  440.             i = (daylight && timeptr->tm_isdst > 0); /* 0 or 1 */
  441.             strcpy(tbuf, tzname[i]);
  442. #else
  443. #ifdef HAVE_TM_ZONE
  444.             strcpy(tbuf, timeptr->tm_zone);
  445. #else
  446. #ifdef HAVE_TM_NAME
  447.             strcpy(tbuf, timeptr->tm_name);
  448. #else
  449.             gettimeofday(& tv, & zone);
  450.             strcpy(tbuf, timezone(zone.tz_minuteswest,
  451.                         timeptr->tm_isdst > 0));
  452. #endif /* HAVE_TM_NAME */
  453. #endif /* HAVE_TM_ZONE */
  454. #endif /* HAVE_TZNAME */
  455.             break;
  456.  
  457. #ifdef SYSV_EXT
  458.         case 'n':    /* same as \n */
  459.             tbuf[0] = '\n';
  460.             tbuf[1] = '\0';
  461.             break;
  462.  
  463.         case 't':    /* same as \t */
  464.             tbuf[0] = '\t';
  465.             tbuf[1] = '\0';
  466.             break;
  467.  
  468.         case 'D':    /* date as %m/%d/%y */
  469.             strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
  470.             break;
  471.  
  472.         case 'e':    /* day of month, blank padded */
  473.             sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
  474.             break;
  475.  
  476.         case 'r':    /* time as %I:%M:%S %p */
  477.             strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
  478.             break;
  479.  
  480.         case 'R':    /* time as %H:%M */
  481.             strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
  482.             break;
  483.  
  484.         case 'T':    /* time as %H:%M:%S */
  485.             strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
  486.             break;
  487. #endif
  488.  
  489. #ifdef SUNOS_EXT
  490.         case 'k':    /* hour, 24-hour clock, blank pad */
  491.             sprintf(tbuf, "%2d", range(0, timeptr->tm_hour, 23));
  492.             break;
  493.  
  494.         case 'l':    /* hour, 12-hour clock, 1 - 12, blank pad */
  495.             i = range(0, timeptr->tm_hour, 23);
  496.             if (i == 0)
  497.                 i = 12;
  498.             else if (i > 12)
  499.                 i -= 12;
  500.             sprintf(tbuf, "%2d", i);
  501.             break;
  502. #endif
  503.  
  504.  
  505. #ifdef VMS_EXT
  506.         case 'v':    /* date as dd-bbb-YYYY */
  507.             sprintf(tbuf, "%02d-%3.3s-%4d",
  508.                 range(1, timeptr->tm_mday, 31),
  509.                 months_a[range(0, timeptr->tm_mon, 11)],
  510.                 timeptr->tm_year + 1900);
  511.             for (i = 3; i < 6; i++)
  512.                 if (islower(tbuf[i]))
  513.                     tbuf[i] = toupper(tbuf[i]);
  514.             break;
  515. #endif
  516.  
  517.  
  518. #ifdef POSIX2_DATE
  519.         case 'C':
  520.             sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
  521.             break;
  522.  
  523.  
  524.         case 'E':
  525.         case 'O':
  526.             /* POSIX locale extensions, ignored for now */
  527.             goto again;
  528.  
  529.         case 'V':    /* week of year according ISO 8601 */
  530.             sprintf(tbuf, "%02d", iso8601wknum(timeptr));
  531.             break;
  532.  
  533.         case 'u':
  534.         /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
  535.             sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 :
  536.                     timeptr->tm_wday);
  537.             break;
  538. #endif    /* POSIX2_DATE */
  539.  
  540. #ifdef ISO_DATE_EXT
  541.         case 'G':
  542.         case 'g':
  543.             /*
  544.              * Year of ISO week.
  545.              *
  546.              * If it's December but the ISO week number is one,
  547.              * that week is in next year.
  548.              * If it's January but the ISO week number is 52 or
  549.              * 53, that week is in last year.
  550.              * Otherwise, it's this year.
  551.              */
  552.             w = iso8601wknum(timeptr);
  553.             if (timeptr->tm_mon == 11 && w == 1)
  554.                 y = 1900 + timeptr->tm_year + 1;
  555.             else if (timeptr->tm_mon == 0 && w >= 52)
  556.                 y = 1900 + timeptr->tm_year - 1;
  557.             else
  558.                 y = 1900 + timeptr->tm_year;
  559.  
  560.             if (*format == 'G')
  561.                 sprintf(tbuf, "%d", y);
  562.             else
  563.                 sprintf(tbuf, "%02d", y % 100);
  564.             break;
  565. #endif ISO_DATE_EXT
  566.         default:
  567.             tbuf[0] = '%';
  568.             tbuf[1] = *format;
  569.             tbuf[2] = '\0';
  570.             break;
  571.         }
  572.         i = strlen(tbuf);
  573.         if (i) {
  574.             if (s + i < endp - 1) {
  575.                 strcpy(s, tbuf);
  576.                 s += i;
  577.             } else
  578.                 return 0;
  579.         }
  580.     }
  581. out:
  582.     if (s < endp && *format == '\0') {
  583.         *s = '\0';
  584.         return (s - start);
  585.     } else
  586.         return 0;
  587. }
  588.  
  589. /* isleap --- is a year a leap year? */
  590.  
  591. #ifndef __STDC__
  592. static int
  593. isleap(year)
  594. int year;
  595. #else
  596. static int
  597. isleap(int year)
  598. #endif
  599. {
  600.     return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
  601. }
  602.  
  603.  
  604. #ifdef POSIX2_DATE
  605. /* iso8601wknum --- compute week number according to ISO 8601 */
  606.  
  607. #ifndef __STDC__
  608. static int
  609. iso8601wknum(timeptr)
  610. const struct tm *timeptr;
  611. #else
  612. static int
  613. iso8601wknum(const struct tm *timeptr)
  614. #endif
  615. {
  616.     /*
  617.      * From 1003.2:
  618.      *    If the week (Monday to Sunday) containing January 1
  619.      *    has four or more days in the new year, then it is week 1;
  620.      *    otherwise it is the highest numbered week of the previous
  621.      *    year (52 or 53), and the next week is week 1.
  622.      *
  623.      * ADR: This means if Jan 1 was Monday through Thursday,
  624.      *    it was week 1, otherwise week 52 or 53.
  625.      *
  626.      * XPG4 erroneously included POSIX.2 rationale text in the
  627.      * main body of the standard. Thus it requires week 53.
  628.      */
  629.  
  630.     int weeknum, jan1day, diff;
  631.  
  632.     /* get week number, Monday as first day of the week */
  633.     weeknum = weeknumber(timeptr, 1);
  634.  
  635.     /*
  636.      * With thanks and tip of the hatlo to tml@tik.vtt.fi
  637.      *
  638.      * What day of the week does January 1 fall on?
  639.      * We know that
  640.      *    (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
  641.      *        (timeptr->tm_wday - jan1.tm_wday) MOD 7
  642.      * and that
  643.      *     jan1.tm_yday == 0
  644.      * and that
  645.      *     timeptr->tm_wday MOD 7 == timeptr->tm_wday
  646.      * from which it follows that. . .
  647.       */
  648.     jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
  649.     if (jan1day < 0)
  650.         jan1day += 7;
  651.  
  652.     /*
  653.      * If Jan 1 was a Monday through Thursday, it was in
  654.      * week 1.  Otherwise it was last year's highest week, which is
  655.      * this year's week 0.
  656.      *
  657.      * What does that mean?
  658.      * If Jan 1 was Monday, the week number is exactly right, it can
  659.      *    never be 0.
  660.      * If it was Tuesday through Thursday, the weeknumber is one
  661.      *    less than it should be, so we add one.
  662.      * Otherwise, Friday, Saturday or Sunday, the week number is
  663.      * OK, but if it is 0, it needs to be 52 or 53.
  664.      */
  665.     switch (jan1day) {
  666.     case 1:        /* Monday */
  667.         break;
  668.     case 2:        /* Tuesday */
  669.     case 3:        /* Wednesday */
  670.     case 4:        /* Thursday */
  671.         weeknum++;
  672.         break;
  673.     case 5:        /* Friday */
  674.     case 6:        /* Saturday */
  675.     case 0:        /* Sunday */
  676.         if (weeknum == 0) {
  677. #ifdef USE_BROKEN_XPG4
  678.             /* XPG4 (as of March 1994) says 53 unconditionally */
  679.             weeknum = 53;
  680. #else
  681.             /* get week number of last week of last year */
  682.             struct tm dec31ly;    /* 12/31 last year */
  683.             dec31ly = *timeptr;
  684.             dec31ly.tm_year--;
  685.             dec31ly.tm_mon = 11;
  686.             dec31ly.tm_mday = 31;
  687.             dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
  688.             dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900);
  689.             weeknum = iso8601wknum(& dec31ly);
  690. #endif
  691.         }
  692.         break;
  693.     }
  694.  
  695.     if (timeptr->tm_mon == 11) {
  696.         /*
  697.          * The last week of the year
  698.          * can be in week 1 of next year.
  699.          * Sigh.
  700.          *
  701.          * This can only happen if
  702.          *    M   T  W
  703.          *    29  30 31
  704.          *    30  31
  705.          *    31
  706.          */
  707.         int wday, mday;
  708.  
  709.         wday = timeptr->tm_wday;
  710.         mday = timeptr->tm_mday;
  711.         if (   (wday == 1 && (mday >= 29 && mday <= 31))
  712.             || (wday == 2 && (mday == 30 || mday == 31))
  713.             || (wday == 3 &&  mday == 31))
  714.             weeknum = 1;
  715.     }
  716.  
  717.     return weeknum;
  718. }
  719. #endif
  720.  
  721. /* weeknumber --- figure how many weeks into the year */
  722.  
  723. /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
  724.  
  725. #ifndef __STDC__
  726. static int
  727. weeknumber(timeptr, firstweekday)
  728. const struct tm *timeptr;
  729. int firstweekday;
  730. #else
  731. static int
  732. weeknumber(const struct tm *timeptr, int firstweekday)
  733. #endif
  734. {
  735.     int wday = timeptr->tm_wday;
  736.     int ret;
  737.  
  738.     if (firstweekday == 1) {
  739.         if (wday == 0)    /* sunday */
  740.             wday = 6;
  741.         else
  742.             wday--;
  743.     }
  744.     ret = ((timeptr->tm_yday + 7 - wday) / 7);
  745.     if (ret < 0)
  746.         ret = 0;
  747.     return ret;
  748. }
  749.  
  750. #if 0
  751. /* ADR --- I'm loathe to mess with ado's code ... */
  752.  
  753. Date:         Wed, 24 Apr 91 20:54:08 MDT
  754. From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
  755. To: arnold@audiofax.com
  756.  
  757. Hi Arnold,
  758. in a process of fixing of strftime() in libraries on Atari ST I grabbed
  759. some pieces of code from your own strftime.  When doing that it came
  760. to mind that your weeknumber() function compiles a little bit nicer
  761. in the following form:
  762. /*
  763.  * firstweekday is 0 if starting in Sunday, non-zero if in Monday
  764.  */
  765. {
  766.     return (timeptr->tm_yday - timeptr->tm_wday +
  767.         (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
  768. }
  769. How nicer it depends on a compiler, of course, but always a tiny bit.
  770.  
  771.    Cheers,
  772.    Michal
  773.    ntomczak@vm.ucs.ualberta.ca
  774. #endif
  775.  
  776. #ifdef    TEST_STRFTIME
  777.  
  778. /*
  779.  * NAME:
  780.  *    tst
  781.  *
  782.  * SYNOPSIS:
  783.  *    tst
  784.  *
  785.  * DESCRIPTION:
  786.  *    "tst" is a test driver for the function "strftime".
  787.  *
  788.  * OPTIONS:
  789.  *    None.
  790.  *
  791.  * AUTHOR:
  792.  *    Karl Vogel
  793.  *    Control Data Systems, Inc.
  794.  *    vogelke@c-17igp.wpafb.af.mil
  795.  *
  796.  * BUGS:
  797.  *    None noticed yet.
  798.  *
  799.  * COMPILE:
  800.  *    cc -o tst -DTEST_STRFTIME strftime.c
  801.  */
  802.  
  803. /* ADR: I reformatted this to my liking, and deleted some unneeded code. */
  804.  
  805. #ifndef NULL
  806. #include    <stdio.h>
  807. #endif
  808. #include    <sys/time.h>
  809. #include    <string.h>
  810.  
  811. #define        MAXTIME        132
  812.  
  813. /*
  814.  * Array of time formats.
  815.  */
  816.  
  817. static char *array[] =
  818. {
  819.     "(%%A)      full weekday name, var length (Sunday..Saturday)  %A",
  820.     "(%%B)       full month name, var length (January..December)  %B",
  821.     "(%%C)                                               Century  %C",
  822.     "(%%D)                                       date (%%m/%%d/%%y)  %D",
  823.     "(%%E)                           Locale extensions (ignored)  %E",
  824.     "(%%H)                          hour (24-hour clock, 00..23)  %H",
  825.     "(%%I)                          hour (12-hour clock, 01..12)  %I",
  826.     "(%%M)                                       minute (00..59)  %M",
  827.     "(%%O)                           Locale extensions (ignored)  %O",
  828.     "(%%R)                                 time, 24-hour (%%H:%%M)  %R",
  829.     "(%%S)                                       second (00..61)  %S",
  830.     "(%%T)                              time, 24-hour (%%H:%%M:%%S)  %T",
  831.     "(%%U)    week of year, Sunday as first day of week (00..53)  %U",
  832.     "(%%V)                    week of year according to ISO 8601  %V",
  833.     "(%%W)    week of year, Monday as first day of week (00..53)  %W",
  834.     "(%%X)     appropriate locale time representation (%H:%M:%S)  %X",
  835.     "(%%Y)                           year with century (1970...)  %Y",
  836.     "(%%Z) timezone (EDT), or blank if timezone not determinable  %Z",
  837.     "(%%a)          locale's abbreviated weekday name (Sun..Sat)  %a",
  838.     "(%%b)            locale's abbreviated month name (Jan..Dec)  %b",
  839.     "(%%c)           full date (Sat Nov  4 12:02:33 1989)%n%t%t%t  %c",
  840.     "(%%d)                             day of the month (01..31)  %d",
  841.     "(%%e)               day of the month, blank-padded ( 1..31)  %e",
  842.     "(%%h)                                should be same as (%%b)  %h",
  843.     "(%%j)                            day of the year (001..366)  %j",
  844.     "(%%k)               hour, 24-hour clock, blank pad ( 0..23)  %k",
  845.     "(%%l)               hour, 12-hour clock, blank pad ( 0..12)  %l",
  846.     "(%%m)                                        month (01..12)  %m",
  847.     "(%%p)              locale's AM or PM based on 12-hour clock  %p",
  848.     "(%%r)                   time, 12-hour (same as %%I:%%M:%%S %%p)  %r",
  849.     "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7]   %u",
  850.     "(%%v)                                VMS date (dd-bbb-YYYY)  %v",
  851.     "(%%w)                       day of week (0..6, Sunday == 0)  %w",
  852.     "(%%x)                appropriate locale date representation  %x",
  853.     "(%%y)                      last two digits of year (00..99)  %y",
  854.     "(%%z)      timezone offset east of GMT as HHMM (e.g. -0500)  %z",
  855.     (char *) NULL
  856. };
  857.  
  858. /* main routine. */
  859.  
  860. int
  861. main(argc, argv)
  862. int argc;
  863. char **argv;
  864. {
  865.     long time();
  866.  
  867.     char *next;
  868.     char string[MAXTIME];
  869.  
  870.     int k;
  871.     int length;
  872.  
  873.     struct tm *tm;
  874.  
  875.     long clock;
  876.  
  877.     /* Call the function. */
  878.  
  879.     clock = time((long *) 0);
  880.     tm = localtime(&clock);
  881.  
  882.     for (k = 0; next = array[k]; k++) {
  883.         length = strftime(string, MAXTIME, next, tm);
  884.         printf("%s\n", string);
  885.     }
  886.  
  887.     exit(0);
  888. }
  889. #endif    /* TEST_STRFTIME */
  890.